#include <fstream>
#include <Format.h>
#include "teacher.h"
#include "comm.h"
#include "global_const.h"

Teacher::Teacher()
{
}

Teacher::Teacher(int id, string name, string password)
{
}

Teacher::~Teacher()
{
}

void Teacher::menu()
{
    cout << "\t\t ----------------------------------\n";
    cout << "\t\t|                                  |\n";
    cout << "\t\t|          1.查看所有预约          |\n";
    cout << "\t\t|                                  |\n";
    cout << "\t\t|          2.审核预约              |\n";
    cout << "\t\t|                                  |\n";
    cout << "\t\t|          0.注销登录              |\n";
    cout << "\t\t|                                  |\n";
    cout << "\t\t ----------------------------------\n";
    cout << "请选择您的操作： " << endl;
}

void Teacher::action(int choice)
{
    switch (choice)
    {
    case 1:
        search_all_rsvt();
        break;
    case 2:
        review_rsvt();
        break;
    default:
        break;
    }
}

void Teacher::search_all_rsvt()
{
    ifstream ifs(RESERVATION_FILE, ios::in);
    if (!ifs.is_open())
    {
        cout << "无预约记录" << endl;
        return;
    }
    string buff;
    string status;
    string interval;
    vector<string> vs;
    while (getline(ifs, buff))
    {
        vs = str_split(buff, ',');
        interval = vs[3];
        status = vs[5];
        if (interval != "Interval")
        {
            interval = interval == "1" ? "上午" : "下午";
            if (status == "0")
                status = "预约成功";
            else if (status == "1")
                status = "待审核";
            else if (status == "2")
                status = "审核未通过";
            else if (status == "9")
                status = "已取消";
        }
        cout << util::Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t", vs[0], vs[1], vs[2], interval, vs[4], status) << endl;
    }
}

void Teacher::review_rsvt()
{
    search_all_rsvt();

    fstream iofs(RESERVATION_FILE, ios::in | ios::out);
    if (!iofs.is_open())
        return;

    cout << "请输入要审核的记录(行号)" << endl;
    int choice;
    cin >> choice;

    cout << "请输入审核意见(0-通过，1-不通过)" << endl;
    int status;
    cin >> status;

    int count = 0;
    string buff;
    while (getline(iofs, buff))
    {
        if (count == choice)
        {
            iofs.seekp(iofs.tellg() - std::streamoff(2));
            if (status == 0)
                iofs.write("0", 1);
            else if (status == 1)
                iofs.write("2", 1);
            break;
        }
        count++;
    }
    iofs.close();
    cout << "审核完成" << endl;
    search_all_rsvt();
}